package com.example.yijian.edit.view;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yijian.R;
import com.example.yijian.edit.EditTypeListener;
import com.example.yijian.edit.adapter.AssetRecyclerViewAdapter;
import com.example.yijian.edit.clipEdit.adjust.AdjustCropActivity;
import com.example.yijian.edit.data.AssetInfoDescription;
import com.example.yijian.edit.data.ChangeSpeedCurveInfo;
import com.example.yijian.edit.view.popup.AdjustPopup;
import com.example.yijian.edit.view.popup.CanvasColorPopup;
import com.example.yijian.edit.view.popup.CanvasStylePopup;
import com.example.yijian.edit.view.popup.CanvasVaguePopup;
import com.example.yijian.edit.view.popup.FilterPopup;
import com.example.yijian.edit.view.popup.SpeedCurvePopup;
import com.example.yijian.edit.view.popup.SpeedNormalPopup;
import com.example.yijian.edit.view.popup.StickerAnimationPopup;
import com.example.yijian.edit.view.popup.VideoAnimationPopup;
import com.example.yijian.edit.view.popup.VolumePopup;
import com.example.yijian.utils.AppManager;
import com.example.yijian.utils.datainfo.ClipInfo;
import com.meicam.sdk.NvsVideoClip;

import java.util.ArrayList;

public class BottomEditTypeView extends RelativeLayout {
    public static final int SPEED_NORMAL = 0;
    public static final int SPEED_CURVE = 1;
    public static final int VOLUME = 2;
    public static final int CANVAS_COLOR = 3;
    public static final int CANVAS_STYLE = 4;
    public static final int CANVAS_VAGUE = 5;

    private RecyclerView mAssetRecycleView;
    private RecyclerView editRecycleList;
    private LinearLayout llNextStep;
    private RelativeLayout rlBack;
    private LinearLayout vBianji;
    private LinearLayout vSpeed;
    private LinearLayout vSpeedNormal;
    private LinearLayout vSpeedCurve;
    private LinearLayout vAnimation;
    private LinearLayout llAnimationIn;
    private LinearLayout llAnimationOut;
    private LinearLayout llAnimationCompany;
    private LinearLayout vLvjing;
    private LinearLayout vTexiao;
    private LinearLayout vTiezhi;
    private LinearLayout vShuiyin;
    private LinearLayout mWaterMark;
    private LinearLayout mEffect;
    private LinearLayout vYinpin;
    private LinearLayout vHuazhonghua;
    private LinearLayout vBeijing;
    private LinearLayout mCanvasColor;
    private LinearLayout mCanvasStyle;
    private LinearLayout mCanvasVague;
    private LinearLayout vBili;
    private Context mContext;
    int[] videoEditImageId = {
            R.mipmap.icon_jianji,
            R.mipmap.icon_lvjing,
            R.mipmap.icon_texiao,
            R.mipmap.icon_tiezhi,
            R.mipmap.icon_zimu,
            R.mipmap.icon_zuhezimu,
            R.mipmap.icon_shuiyin,
            R.mipmap.icon_yinpin,
            R.mipmap.icon_luzhi,
            R.mipmap.icon_huazhonghua,
            R.mipmap.icon_beijing,
            R.mipmap.icon_tiaojie,
            R.mipmap.icon_bili};
    int[] editImageIds = {
            R.mipmap.icon_fenge,
            R.mipmap.icon_biansu,
            R.mipmap.icon_yinliang,
            R.mipmap.icon_donghua,
            R.mipmap.icon_shanchu,
            R.mipmap.icon_kouxiang,
            R.mipmap.icon_daoju,
            R.mipmap.icon_mengban,
            R.mipmap.icon_biansheng,
            R.mipmap.icon_lvjing,
            R.mipmap.icon_tiaojie,
            R.mipmap.icon_toumingdu,
            R.mipmap.icon_jiaodu,
            R.mipmap.icon_caijian,
            R.mipmap.icon_jingxiang,
            R.mipmap.icon_fuzhi,
            R.mipmap.icon_daofang,
            R.mipmap.icon_dingge,
            R.mipmap.icon_meifu};
    private AssetRecyclerViewAdapter mAssetRecycleAdapter;
    private AssetRecyclerViewAdapter mEditRecycleAdapter;
    private ArrayList<AssetInfoDescription> mArrayAssetInfo;
    private ArrayList<AssetInfoDescription> mArrayEditInfo;
    private View rootView;

    private EditTypeListener mEditListener;

    private SpeedNormalPopup mSpeedNormalPopup;
    private SpeedCurvePopup mSpeedCurvePopup;
    private VolumePopup mVolumePopup;
    private AdjustPopup mAdjustPopup;
    private FilterPopup mFilterPopup;
    private CanvasColorPopup mCanvasColorPopup;
    private CanvasStylePopup mCanvasStylePopup;
    private CanvasVaguePopup mCanvasVaguePopup;
    private VideoAnimationPopup mVideoAnimationPopup;


    public void setEditListener(EditTypeListener mEditListener) {
        this.mEditListener = mEditListener;
    }

    public BottomEditTypeView(Context context) {
        super(context);
        mContext = context;
        this.init();
    }

    public BottomEditTypeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        this.init();
    }

    public BottomEditTypeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        this.init();
    }

    private void init() {
        rootView = LayoutInflater.from(mContext).inflate(R.layout.view_bottom_edit_type, this);
        initView();
        initAssetInfo();
        initAssetRecycleAdapter();
        initEditInfo();
        initEditRecycleAdapter();
        initListener();
    }

    private void initListener() {

    }

    private void initEditRecycleAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        editRecycleList.setLayoutManager(layoutManager);
        mEditRecycleAdapter = new AssetRecyclerViewAdapter(mArrayEditInfo);
        editRecycleList.setAdapter(mEditRecycleAdapter);
        mEditRecycleAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mEditListener == null) {
                return;
            }
            String tag = (String) view.getTag();
            switch (tag) {
                case "分割":
                    mEditListener.split();
                    break;
                case "变速":
                    vBianji.setVisibility(GONE);
                    vSpeed.setVisibility(VISIBLE);
                    break;
                case "音量":
                    mEditListener.speedStart(VOLUME);
                    break;
                case "动画":
                    vBianji.setVisibility(GONE);
                    vAnimation.setVisibility(VISIBLE);
                    mEditListener.animation();
                    break;
                case "删除":
                    mEditListener.delete();
                    break;
                case "智能扣像":
                    break;
                case "蒙版":
                    mEditListener.mask();
                    break;
                case "变声":
                    break;
                case "滤镜":
                    showFilterPopup();
                    break;
                case "调节":
                    mEditListener.adjustStart();
                    break;
                case "透明度":
                    break;
                case "角度":
                    mEditListener.onRotation();
                    break;
                case "裁剪":
                    AppManager.getInstance().jumpActivityForResult((Activity) mContext, AdjustCropActivity.class, new Bundle(), 200);
                    break;
                case "镜像":
                    mEditListener.onHorizontal();
                    break;
                case "复制":
                    mEditListener.copy();
                    break;
                case "倒放":
                    mEditListener.reverse();
                    break;
                case "定格":
                    break;
                case "美肤":
                    break;
            }
        });
    }

    private void initEditInfo() {
        mArrayEditInfo = new ArrayList<>();
        String[] assetName = getResources().getStringArray(R.array.editTypes);
        for (int i = 0; i < assetName.length; i++) {
            mArrayEditInfo.add(new AssetInfoDescription(assetName[i], editImageIds[i]));
        }
    }

    private void initView() {
        mAssetRecycleView = rootView.findViewById(R.id.assetRecycleList);
        editRecycleList = rootView.findViewById(R.id.editRecycleList);
        llNextStep = rootView.findViewById(R.id.ll_next_step);
        rlBack = rootView.findViewById(R.id.rl_back);
        vBianji = rootView.findViewById(R.id.view_bianji);
        vSpeed = rootView.findViewById(R.id.view_speed);
        vSpeedNormal = rootView.findViewById(R.id.ll_speed_normal);
        vSpeedCurve = rootView.findViewById(R.id.ll_speed_curve);
        vAnimation = rootView.findViewById(R.id.view_animation);
        llAnimationIn = rootView.findViewById(R.id.ll_animation_in);
        llAnimationOut = rootView.findViewById(R.id.ll_animation_out);
        llAnimationCompany = rootView.findViewById(R.id.ll_animation_company);
        vLvjing = rootView.findViewById(R.id.view_lvjing);
        vTexiao = rootView.findViewById(R.id.view_texiao);
        vTiezhi = rootView.findViewById(R.id.view_tiezhi);
        vShuiyin = rootView.findViewById(R.id.view_shuiyin);
        mWaterMark = rootView.findViewById(R.id.ll_watermark);
        mEffect = rootView.findViewById(R.id.ll_effect);
        vYinpin = rootView.findViewById(R.id.view_yinpin);
        vHuazhonghua = rootView.findViewById(R.id.view_huazhonghua);
        vBeijing = rootView.findViewById(R.id.view_beijing);
        mCanvasColor = rootView.findViewById(R.id.ll_canvas_color);
        mCanvasStyle = rootView.findViewById(R.id.ll_canvas_style);
        mCanvasVague = rootView.findViewById(R.id.ll_canvas_vague);
        vBili = rootView.findViewById(R.id.view_bili);
        rlBack.setOnClickListener(v -> {
            if (vSpeed.getVisibility() == VISIBLE) {
                vSpeed.setVisibility(GONE);
                vBianji.setVisibility(VISIBLE);
                return;
            }
            if (vAnimation.getVisibility() == VISIBLE) {
                vAnimation.setVisibility(GONE);
                vBianji.setVisibility(VISIBLE);
                return;
            }
            updateView();
            llNextStep.setVisibility(GONE);
            if (mEditListener != null) {
                mEditListener.back();
            }
        });
        llNextStep.setOnClickListener(v -> {
        });
        vSpeedNormal.setOnClickListener(v -> mEditListener.speedStart(SPEED_NORMAL));
        vSpeedCurve.setOnClickListener(v -> mEditListener.speedStart(SPEED_CURVE));
        mCanvasColor.setOnClickListener(v -> mEditListener.speedStart(CANVAS_COLOR));
        mCanvasStyle.setOnClickListener(v -> mEditListener.speedStart(CANVAS_STYLE));
        mCanvasVague.setOnClickListener(v -> mEditListener.speedStart(CANVAS_VAGUE));
        mWaterMark.setOnClickListener(v -> mEditListener.waterMark());
        mEffect.setOnClickListener(v -> mEditListener.effect());
        llAnimationIn.setOnClickListener(v -> showAnimationPopup(0));
        llAnimationOut.setOnClickListener(v -> showAnimationPopup(1));
        llAnimationCompany.setOnClickListener(v -> showAnimationPopup(2));
    }

    public void showSpeedPopup(ClipInfo clipInfo, int type) {
        switch (type) {
            case SPEED_NORMAL:
                if (mSpeedNormalPopup == null) {
                    mSpeedNormalPopup = new SpeedNormalPopup(mContext);
                }
                mSpeedNormalPopup.setOnFunctionListener((speed, changeVoice) -> mEditListener.speedNormal(speed, changeVoice));
                mSpeedNormalPopup.setData(clipInfo);
                mSpeedNormalPopup.showPopupWindow();
                break;
            case SPEED_CURVE:
                if (mSpeedCurvePopup == null) {
                    mSpeedCurvePopup = new SpeedCurvePopup(mContext);
                }
                mSpeedCurvePopup.setOnFunctionListener(new SpeedCurvePopup.OnFunctionListener() {
                    @Override
                    public void onEditCurve(ChangeSpeedCurveInfo changeSpeedCurveInfo) {
                        mEditListener.editCurve(changeSpeedCurveInfo);
                    }

                    @Override
                    public void onSpeedChanged(ChangeSpeedCurveInfo changeSpeedCurveInfo) {
                        mEditListener.speedCurve(changeSpeedCurveInfo);
                    }
                });
                mSpeedCurvePopup.setData(clipInfo);
                mSpeedCurvePopup.showPopupWindow();
                break;
            case VOLUME:
                if (mVolumePopup == null) {
                    mVolumePopup = new VolumePopup(mContext);
                }
                mVolumePopup.setOnFunctionListener(volume -> mEditListener.volume(volume));
                mVolumePopup.setOutSideTouchable(false);
                mVolumePopup.setData(clipInfo);
                mVolumePopup.showPopupWindow();
                break;
            case CANVAS_COLOR:
                if (mCanvasColorPopup == null) {
                    mCanvasColorPopup = new CanvasColorPopup(mContext);
                }
                mCanvasColorPopup.setOnMultiColorItemClickListener((userAll, colorInfo) -> {
                    if (mEditListener != null) {
                        mEditListener.canvasColor(userAll, colorInfo);
                    }
                });
                mCanvasColorPopup.setData(clipInfo);
                mCanvasColorPopup.showPopupWindow();
                break;
            case CANVAS_STYLE:
                if (mCanvasStylePopup == null) {
                    mCanvasStylePopup = new CanvasStylePopup(mContext);
                }
                mCanvasStylePopup.setOnBackgroundStyleItemClickListener((userAll, position, backgroundStyleInfo) -> {
                    if (mEditListener != null) {
                        mEditListener.canvasStyle(userAll, position, backgroundStyleInfo);
                    }
                });
                mCanvasStylePopup.setData(clipInfo);
                mCanvasStylePopup.showPopupWindow();
                break;
            case CANVAS_VAGUE:
                if (mCanvasVaguePopup == null) {
                    mCanvasVaguePopup = new CanvasVaguePopup(mContext);
                }
                mCanvasVaguePopup.setOnBackgroundBlurItemClickListener((userAll, strength) -> mEditListener.canvasVague(userAll, strength));
                mCanvasVaguePopup.setData(clipInfo);
                mCanvasVaguePopup.showPopupWindow();
                break;
            default:
                break;
        }
    }

    public void showAdjustPopup(ClipInfo clipInfo, NvsVideoClip videoClip) {
        if (mAdjustPopup == null) {
            mAdjustPopup = new AdjustPopup(mContext);
        }
        mAdjustPopup.setOnFunctionListener((colorVal, mCurrentColorType) -> mEditListener.adjustChange(colorVal, mCurrentColorType));
        mAdjustPopup.setData(clipInfo, videoClip);
        mAdjustPopup.showPopupWindow();
    }

    public void showFilterPopup() {
        if (mFilterPopup == null) {
            mFilterPopup = new FilterPopup(mContext);
        }
        mFilterPopup.showPopupWindow();
    }

    public void showAnimationPopup(int position) {
        if (mVideoAnimationPopup == null) {
            mVideoAnimationPopup = new VideoAnimationPopup(mContext);
        }
        mVideoAnimationPopup.initData(position);
        mVideoAnimationPopup.showPopupWindow();
    }

    private void initAssetInfo() {
        mArrayAssetInfo = new ArrayList<>();
        String[] assetName = getResources().getStringArray(R.array.videoEdit);
        for (int i = 0; i < assetName.length; i++) {
            mArrayAssetInfo.add(new AssetInfoDescription(assetName[i], videoEditImageId[i]));
        }
    }

    private void initAssetRecycleAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        mAssetRecycleView.setLayoutManager(layoutManager);
        mAssetRecycleAdapter = new AssetRecyclerViewAdapter(mArrayAssetInfo);
        mAssetRecycleView.setAdapter(mAssetRecycleAdapter);
        mAssetRecycleAdapter.setOnItemClickListener((adapter, view, position) -> {
            String tag = (String) view.getTag();
            llNextStep.setVisibility(VISIBLE);
            updateView();
            switch (tag) {
                case "编辑":
                    if (mEditListener != null) {
                        mEditListener.edit();
                    }
                    vBianji.setVisibility(VISIBLE);
                    break;
                case "滤镜":
                case "调节":
                    vLvjing.setVisibility(VISIBLE);
                    break;
                case "特效":
                    vTexiao.setVisibility(VISIBLE);
                    break;
                case "贴纸":
                case "字幕":
                case "组合字幕":
                    vTiezhi.setVisibility(VISIBLE);
                    break;
                case "水印":
                    vShuiyin.setVisibility(VISIBLE);
                    break;
                case "音频":
                case "录制":
                    vYinpin.setVisibility(VISIBLE);
                    break;
                case "画中画":
                    vHuazhonghua.setVisibility(VISIBLE);
                    break;
                case "背景":
                    vBeijing.setVisibility(VISIBLE);
                    break;
                case "比例":
                    vBili.setVisibility(VISIBLE);
                    break;
            }
        });
    }

    private void updateView() {
        vBianji.setVisibility(GONE);
        vSpeed.setVisibility(GONE);
        vAnimation.setVisibility(GONE);
        vLvjing.setVisibility(GONE);
        vTexiao.setVisibility(GONE);
        vTiezhi.setVisibility(GONE);
        vShuiyin.setVisibility(GONE);
        vYinpin.setVisibility(GONE);
        vHuazhonghua.setVisibility(GONE);
        vBeijing.setVisibility(GONE);
        vBili.setVisibility(GONE);
    }

    public void showEditType() {
        llNextStep.setVisibility(VISIBLE);
        updateView();
        vBianji.setVisibility(VISIBLE);
    }

    public void resumeEditView() {
        updateView();
        llNextStep.setVisibility(GONE);
    }

}
